package com.cbh.amadeus.common.enums;

import com.cbh.amadeus.common.protocol.AmadeusMessage;
import com.cbh.amadeus.common.protocol.AmadeusMessageWrap;
import com.cbh.amadeus.common.utils.StringUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

/**
 * @author ChenBiHui
 * @date 2025/4/25
 */
@Slf4j
@Getter
@AllArgsConstructor
public enum MessageType {

    /**
     * 注册 申请
     */
    REGISTER(1, msg -> {
        AmadeusMessage message = msg.getMessage();
        if (message == null) {
            log.warn("===== Event [REGISTER], Illegal message [object], Will refuse access =====");
            return true;
        }
        String provider = message.getProvider();
        if (StringUtil.unHasText(provider)) {
            log.warn("===== Event [REGISTER], Illegal message [provider], Will refuse access =====");
            return true;
        }
        String topic = message.getTopic();
        if (StringUtil.unHasText(topic)) {
            log.warn("===== Event [REGISTER], Illegal message [topic], Will refuse access =====");
            return true;
        }
        String payload = message.getPayload();
        if (StringUtil.unHasText(payload)) {
            log.warn("===== Event [REGISTER], Illegal message [payload], Will refuse access =====");
            return true;
        }
        return false;
    }),

    /**
     * 注册 完成
     */
    REGISTERED(2, msg -> {
        AmadeusMessage message = msg.getMessage();
        if (message == null) {
            log.warn("===== Event [REGISTERED], Illegal message [object], Will refuse access =====");
            return true;
        }
        String provider = message.getProvider();
        if (StringUtil.unHasText(provider)) {
            log.warn("===== Event [REGISTERED], Illegal message [provider], Will refuse access =====");
            return true;
        }
        String topic = message.getTopic();
        if (StringUtil.unHasText(topic)) {
            log.warn("===== Event [REGISTERED], Illegal message [topic], Will refuse access =====");
            return true;
        }
        String payload = message.getPayload();
        if (StringUtil.unHasText(payload)) {
            log.warn("===== Event [REGISTERED], Illegal message [payload], Will refuse access =====");
            return true;
        }
        return false;
    }),

    /**
     * 心跳 发送
     */
    BIT_SEND(3, msg -> false),

    /**
     * 心跳 答复
     */
    BIT_BACK(4, msg -> false),

    /**
     * 消息 发布
     */
    GEN(5, msg -> {
        MessageApproach approach = MessageApproach.find(msg.getApproach());
        if (approach == null) {
            log.warn("===== Event [GEN], Illegal message [approach], Will refuse access =====");
            return true;
        }
        AmadeusMessage message = msg.getMessage();
        if (message == null) {
            log.warn("===== Event [GEN], Illegal message [object], Will refuse access =====");
            return true;
        }
        String tenant = message.getTenant();
        if (StringUtil.unHasText(tenant)) {
            log.warn("===== Event [GEN], Illegal message [tenant], Will refuse access =====");
            return true;
        }
        String provider = message.getProvider();
        if (StringUtil.unHasText(provider)) {
            log.warn("===== Event [GEN], Illegal message [provider], Will refuse access =====");
            return true;
        }
        String topic = message.getTopic();
        if (StringUtil.unHasText(topic)) {
            log.warn("===== Event [GEN], Illegal message [topic], Will refuse access =====");
            return true;
        }
        String messageId = message.getMessageId();
        if (StringUtil.unHasText(messageId)) {
            log.warn("===== Event [GEN], Illegal message [messageId], Will refuse access =====");
            return true;
        }
        String payload = message.getPayload();
        if (StringUtil.unHasText(payload)) {
            log.warn("===== Event [GEN], Illegal message [payload], Will refuse access =====");
            return true;
        }
        return false;
    }),

    /**
     * 消息 ACK
     */
    ACK(6, msg -> {
        AmadeusMessage message = msg.getMessage();
        if (message == null) {
            log.warn("===== Event [ACK], Illegal message [object], Will refuse access =====");
            return true;
        }
        String tenant = message.getTenant();
        if (StringUtil.unHasText(tenant)) {
            log.warn("===== Event [ACK], Illegal message [tenant], Will refuse access =====");
            return true;
        }
        String provider = message.getProvider();
        if (StringUtil.unHasText(provider)) {
            log.warn("===== Event [ACK], Illegal message [provider], Will refuse access =====");
            return true;
        }
        String topic = message.getTopic();
        if (StringUtil.unHasText(topic)) {
            log.warn("===== Event [ACK], Illegal message [topic], Will refuse access =====");
            return true;
        }
        String messageId = message.getMessageId();
        if (StringUtil.unHasText(messageId)) {
            log.warn("===== Event [ACK], Illegal message [messageId], Will refuse access =====");
            return true;
        }
        String payload = message.getPayload();
        if (StringUtil.unHasText(payload)) {
            log.warn("===== Event [ACK], Illegal message [payload], Will refuse access =====");
            return true;
        }
        return false;
    }),

    /**
     * 消息 重发
     */
    RESEND(7, msg -> {
        MessageApproach approach = MessageApproach.find(msg.getApproach());
        if (approach == null) {
            log.warn("===== Event [RESEND], Illegal message [approach], Will refuse access =====");
            return true;
        }
        AmadeusMessage message = msg.getMessage();
        if (message == null) {
            log.warn("===== Event [RESEND], Illegal message [object], Will refuse access =====");
            return true;
        }
        String tenant = message.getTenant();
        if (StringUtil.unHasText(tenant)) {
            log.warn("===== Event [RESEND], Illegal message [tenant], Will refuse access =====");
            return true;
        }
        String provider = message.getProvider();
        if (StringUtil.unHasText(provider)) {
            log.warn("===== Event [RESEND], Illegal message [provider], Will refuse access =====");
            return true;
        }
        String topic = message.getTopic();
        if (StringUtil.unHasText(topic)) {
            log.warn("===== Event [RESEND], Illegal message [topic], Will refuse access =====");
            return true;
        }
        String messageId = message.getMessageId();
        if (StringUtil.unHasText(messageId)) {
            log.warn("===== Event [RESEND], Illegal message [messageId], Will refuse access =====");
            return true;
        }
        String payload = message.getPayload();
        if (StringUtil.unHasText(payload)) {
            log.warn("===== Event [RESEND], Illegal message [payload], Will refuse access =====");
            return true;
        }
        return false;
    }),

    /**
     * 消息 释放
     */
    RELEASE(8, msg -> {
        AmadeusMessage message = msg.getMessage();
        if (message == null) {
            log.warn("===== Event [RELEASE], Illegal message [object], Will refuse access =====");
            return true;
        }
        String tenant = message.getTenant();
        if (StringUtil.unHasText(tenant)) {
            log.warn("===== Event [RELEASE], Illegal message [tenant], Will refuse access =====");
            return true;
        }
        String provider = message.getProvider();
        if (StringUtil.unHasText(provider)) {
            log.warn("===== Event [RELEASE], Illegal message [provider], Will refuse access =====");
            return true;
        }
        String topic = message.getTopic();
        if (StringUtil.unHasText(topic)) {
            log.warn("===== Event [RELEASE], Illegal message [topic], Will refuse access =====");
            return true;
        }
        String messageId = message.getMessageId();
        if (StringUtil.unHasText(messageId)) {
            log.warn("===== Event [RELEASE], Illegal message [messageId], Will refuse access =====");
            return true;
        }
        return false;
    }),

    /**
     * 消息 拒收
     */
    REJECT(9, msg -> {
        AmadeusMessage message = msg.getMessage();
        if (message == null) {
            log.warn("===== Event [REJECT], Illegal message [object], Will refuse access =====");
            return true;
        }
        String tenant = message.getTenant();
        if (StringUtil.unHasText(tenant)) {
            log.warn("===== Event [REJECT], Illegal message [tenant], Will refuse access =====");
            return true;
        }
        String provider = message.getProvider();
        if (StringUtil.unHasText(provider)) {
            log.warn("===== Event [REJECT], Illegal message [provider], Will refuse access =====");
            return true;
        }
        String topic = message.getTopic();
        if (StringUtil.unHasText(topic)) {
            log.warn("===== Event [REJECT], Illegal message [topic], Will refuse access =====");
            return true;
        }
        String messageId = message.getMessageId();
        if (StringUtil.unHasText(messageId)) {
            log.warn("===== Event [REJECT], Illegal message [messageId], Will refuse access =====");
            return true;
        }
        return false;
    }),

    /**
     * 只连接 发送(仅作为消息发送者, 不订阅任何主题)
     */
    ONLY_CONNECT_SEND(10, msg -> {
        AmadeusMessage message = msg.getMessage();
        if (message == null) {
            log.warn("===== Event [ONLY CONNECT SEND], Illegal message [object], Will refuse access =====");
            return true;
        }
        String provider = message.getProvider();
        if (StringUtil.unHasText(provider)) {
            log.warn("===== Event [ONLY CONNECT SEND], Illegal message [provider], Will refuse access =====");
            return true;
        }
        return false;
    }),

    /**
     * 只连接 应答(仅记录, 不额外保存对应消息发送者)
     */
    ONLY_CONNECT_BACK(11, msg -> {
        AmadeusMessage message = msg.getMessage();
        if (message == null) {
            log.warn("===== Event [ONLY CONNECT BACK], Illegal message [object], Will refuse access =====");
            return true;
        }
        String provider = message.getProvider();
        if (StringUtil.unHasText(provider)) {
            log.warn("===== Event [ONLY CONNECT BACK], Illegal message [provider], Will refuse access =====");
            return true;
        }
        return false;
    }),

    /**
     * 重复名称
     */
    @Deprecated
    DUPLICATE_NAME(Integer.MIN_VALUE, msg -> false),

    ;

    /**
     * 缓存 时间复杂度O(1)
     */
    private static final Map<Integer, MessageType> CACHE = new HashMap<>(18);

    static {
        for (MessageType value : values()) {
            CACHE.put(value.type, value);
        }
    }

    private final Integer type;
    private final Function<AmadeusMessageWrap, Boolean> identify;

    public static MessageType find(Byte type) {
        return CACHE.get(Byte.toUnsignedInt(type));
    }
}
